package com.centriole.service.workflow.service.shiro;

import com.centriole.common.entity.ExPermission;
import com.centriole.common.entity.ExRole;
import com.centriole.common.entity.ExUser;
import com.centriole.service.workflow.entity.shiro.Permission;
import com.centriole.service.workflow.entity.shiro.Role;
import com.centriole.service.workflow.entity.shiro.ShiroUser;

import java.util.ArrayList;
import java.util.List;

public class UserShiroConvertor {

    public  static List<Role> toIRoleList(List<ExRole> exRoles){
        List<Role> roles = new ArrayList<>();
        for(ExRole r : exRoles){
            List<ExPermission> exPermissions = r.getPermissions();
            List<Permission> permissions = new ArrayList<>();
            if(exPermissions == null ) continue;
            for(ExPermission p : exPermissions){
                if(p==null) continue;
                Permission permission = new Permission(p.getPermissionId(), p.getPermissionResource(), p.getPermissionName(), new String(r.getRoleName()).intern());
                permissions.add( permission );
            }
            Role role = new Role(r.getRoleId(),r.getRoleName(),permissions);
            roles.add(role);
        }
        return roles;
    }

    public static List<ShiroUser> toShiroUserList(List<ExUser> list){
        List<ShiroUser> shiroUsers = new ArrayList<>();

        for(ExUser u: list){
            if(u==null) continue;
            List<Role> roles = toIRoleList(u.getRoles());
            shiroUsers.add( new ShiroUser(u.getUserId() , u.getAccountName(), u.getPassword() , u.getNickName() , u.getStatus()+"" , roles)  );
        }
        return shiroUsers;
    }

}
